<template>
  <div style="padding-top:60px;  margin-left: -10px;;min-width: 380px">
    <div class="uploaderCenter">
      <img class="img" :src="logo">
    </div>
    <div style="padding-top: 8px"/>
    <van-row justify="center">
      <h3>"伙伴匹配——寻找志同道合的伙伴"</h3>
    </van-row>
    <van-divider></van-divider>
    <!--  <van-uploader class="uploaderCenter" :after-read="afterRead"/>-->
    <div>
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <div class="form-item-container">
            <p class="n"></p>
          <van-field
              v-model="username"
              :rules="[{ required: true, message: '请填写昵称!' }]"

              name="昵称"
              placeholder="昵称"
          >
            <template #left-icon>
              <van-icon name="manager-o"/>
            </template>
          </van-field>
          </div>

          <div class="form-item-container">
            <p class="p">*</p>
          <van-field
              left-icon="user-o"
              v-model="userAccount"
              :rules="[{ required: true, message: '请填写账号!' }]"

              name="账号"
              placeholder="账号"
          />
          </div>

          <div class="form-item-container">
            <p class="p">*</p>
            <van-field
                v-model="phone"
                name="phone"
                left-icon="phone-o"
                placeholder="请输入手机号"
                :rules="[{ required: true, message: '请填写手机号' }]"
            />
          </div>

          <div class="form-item-container">
            <p class="p">*</p>
            <van-field
                v-model="email"
                name="email"
                left-icon="envelop-o"
                placeholder="请输入邮箱"
                :rules="[{ required: true, message: '请填写邮箱' }]"
            />
          </div>

          <div class="form-item-container">
            <p class="p">*</p>
          <van-field
              v-model="password"
              :rules="[{ required: true, message: '请填写密码!' }]"
              name="密码"
              placeholder="密码"
              type="password"
          >
            <template #left-icon>
              <van-icon name="lock"/>
            </template>
          </van-field>
          </div>


          <div class="form-item-container">
            <p class="p">*</p>
          <van-field
              v-model="checkPassword"
              :rules="[{ required: true, message: '请确认密码!' }]"
              name="确认密码"
              placeholder="确认密码"
              type="password"
          >
            <template #left-icon>
              <van-icon name="lock"/>
            </template>
          </van-field>
          </div>

          <van-radio-group v-model="gender" direction="horizontal" icon-size="16px" >
            <van-radio class="radio-label" name="0">♂</van-radio>
            <van-radio class="radio-label" name="1">♀</van-radio>
          </van-radio-group>
          <div class="longin">
            <van-button block class="defaultLogin" native-type="submit" round type="primary">
              注册账号
            </van-button>
            <van-cell title="" to="/user/login" value="已有账号？点击登录"></van-cell>
          </div>
        </van-cell-group>
      </van-form>
    </div>
  </div>
</template>
<script lang="ts" setup>
import {ref} from "vue";
import {useRoute, useRouter} from "vue-router";
import {Toast} from "vant";
/*import {defaultPicture} from "../../components/userCommon";*/
import logo from "../../assets/logo.jpeg";
import myAxios from "../../plugins/myAxios";

const router = useRouter();
const route=useRoute();
const userAccount = ref('');
const username = ref('');
const checkPassword = ref('');
const password = ref('');
const phone=ref('');
const email=ref('');
const gender=ref();
const onSubmit = async () => {
  const res = await myAxios.post("/user/register", {
    "checkPassword": checkPassword.value,
    "userAccount": userAccount.value,
    "userPassword": password.value,
    "username": username.value,
    "phone": phone.value,
    "email": email.value,
    "gender": gender.value,
  })
  console.log(res, '用户注册');
  if (res.code === 0 && res.data) {
    Toast.success('注册成功');
    // 跳转到之前的页面
    const redirectUrl = route.query?.redirect as string ?? '/user/login';
    window.location.href = redirectUrl;
  } else {
    Toast.fail('注册失败');
  }
};
</script>

<style scoped>
@import "../../assets/css/public.css";

.longin {
  margin: 16px 14px 0 5%;
}

.defaultLogin {
  flex: auto;
}

.uploaderCenter {
  margin-top: 8px;
  display: flex;
  justify-content: center;
}

.form-item-container {
  display: flex;
  align-items: center; /* 使图标和输入框垂直居中对齐 */
}

.p{
  margin-left: 10px;
  color: #ee0a24;
  font-size: 1.2em;
}
.radio-label{
  font-weight: bold;
  font-size: 1.2em;
  margin-left: 30px;
  margin-top: 12px;
}
.n{
  margin-left: 20px;
}
</style>
